function calender(options){
    var d = new Date();
    var cfg = {
        oT:false,
        oiframe:false,
        obody:false,
        otable:false,
		Y_span:20,
        yy:d.getFullYear(),
        mm:d.getMonth()+1,
        dd:d.getDate(),
        hh:d.getHours(),
        ii:d.getMinutes(), 
        ss:d.getSeconds(),
        tw:"215px",
        isDrag:false,
        isShowTime:true,
        isShowType:'',
		isCanHiddle:true,
        isIE:navigator.userAgent.indexOf("MSIE")>0
    };
    this.cfg = cfg;
	this.cfg.oiframe = document.createElement("IFRAME");
    document.body.appendChild(this.cfg.oiframe);
    this.cfg.oiframe.id="calenderWidgetIframe";

    this.cfg.obody = document.createElement("DIV");
    document.body.appendChild(this.cfg.obody);
    this.cfg.obody.id="calenderWidget";
    this.cfg.obody.onselectstart = function(){return false}

    this.initTable();


    var _t = this;
    var oHeader = _t.$$("calenderHeader");
    oHeader.onmousedown = function(event){
        _t.cfg.isDrag = true;
        _t.Drag(oHeader,event);
    }
    _t.$$("calender_pre").onclick = function(){
        var y = _t.cfg.yy;
        var m = _t.cfg.mm;
        m=m-1;
        if(m<=0){m=12;y--}
        _t.cfg.yy = y;
        _t.cfg.mm = m;
        _t.onchange(1);
    }
    _t.$$("calender_next").onclick = function(){
        var y = _t.cfg.yy;
        var m = _t.cfg.mm;
        m=m-(-1);
        if(m>12){m=1;y++}
        _t.cfg.yy = y;
        _t.cfg.mm = m;
       _t.onchange(1);
    }
    _t.$$("calender_close").onclick = function(){
        _t.hide();
    }
    _t.$$("calender_clear").onclick = function(){
        _t.cfg.oT.value="";
        _t.hide();
    }
    _t.$$("calender_current").onclick = function(){
		var d = new Date();
        _t.cfg.yy = d.getFullYear();
        _t.cfg.mm = d.getMonth()+1;
        _t.cfg.dd = d.getDate();
        _t.cfg.hh = d.getHours();
        _t.cfg.ii = d.getMinutes();
        _t.cfg.ss = d.getSeconds();
        _t.cfg.oT.value=_t.getSelect();
        _t.hide();
    }
    _t.addEvent(document,"keydown",function(event){
		event = event ||window.event;
		if(event.keyCode==27){
			_t.hide();
		}
	});
   _t.addEvent(document,"click",function(event){
       event = event||window.event;
       var p_el = event.srcElement || event.target;
       var isbody = false;
       while(p_el){
           if(p_el==_t.cfg.obody){
               isbody = true;
               break;
           }
           p_el = p_el.offsetParent;
       }
       if(event.srcElement!=_t.cfg.oT && event.srcElement!=_t.cfg.obody && !isbody){
           _t.hide();
       }
	});
}
calender.prototype.$$ = function(id){
    if(typeof id == "object") return id;
    return document.getElementById(id);
}
calender.prototype.checkNumber = function(num){
    if(num.substring(0,1)==0) return num.substring(1,2);
    return num;
}
calender.prototype.initCalender = function(obj,bTime,attr,bType){//
    var _t = this;
	obj.readOnly=true;
	if(attr){
		for(var k in attr){
			try{
				eval('obj.'+k+'='+attr[k]+';');
			}catch(e){
				try{
					obj.setAttribute(k,attr[k],0);
				}catch(e){}
			}
		}
	}

    obj.onclick=function(){
        _t.cfg.oT = obj;
       if(bTime=='no'){
			_t.cfg.isShowTime = false;
		}else{
			_t.cfg.isShowTime = true;
		}
                if(bType == 'month'){
                    _t.cfg.oT.isShowType = 'month';
                }
        var d = new Date();

        if(_t.trim(obj.value)!=""){
			if(_t.checkValid(obj.value)==false){

			}else{
				if(_t.cfg.isShowTime==true){
					var reg = /^(\d+)-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
					var r = obj.value.match(reg);
					_t.cfg.yy = r[1];
					_t.cfg.mm = _t.checkNumber(r[2]);
					_t.cfg.dd = _t.checkNumber(r[3]);
					_t.cfg.hh = _t.checkNumber(r[4]);
					_t.cfg.ii = _t.checkNumber(r[5]);
					_t.cfg.ss = _t.checkNumber(r[6]);
				}else if(_t.cfg.oT.isShowType == 'month'){
					var reg = /^(\d+)-(\d{1,2})$/;
					var r = obj.value.match(reg);
                                        if(r!=null){
                                            _t.cfg.yy = r[1];
                                            _t.cfg.mm = _t.checkNumber(r[2]);
                                            _t.cfg.dd = d.getDate();
                                        }else{
                                            _t.cfg.yy = obj.value.substring(0,4);
                                            _t.cfg.mm = obj.value.substring(4,6);
                                            _t.cfg.dd = d.getDate();
                                        }
				}else{
					var reg = /^(\d+)-(\d{1,2})-(\d{1,2})$/;
					var r = obj.value.match(reg);
                                        if(r!=null){
                                            _t.cfg.yy = r[1];
                                            _t.cfg.mm = _t.checkNumber(r[2]);
                                            _t.cfg.dd = _t.checkNumber(r[3]);
                                        }else{
                                            _t.cfg.yy = obj.value.substring(0,4);
                                            _t.cfg.mm = obj.value.substring(4,6);
                                            _t.cfg.dd = obj.value.substring(6,8);
                                        }
				}
				var nowY = d.getFullYear();
				if(_t.cfg.yy>nowY+_t.cfg.Y_span || _t.cfg.yy<nowY-_t.cfg.Y_span){
					_t.cfg.yy = nowY;
				}
			}
        }else{
            _t.cfg.yy=d.getFullYear();
            _t.cfg.mm=d.getMonth()+1;
            _t.cfg.dd=d.getDate();
            _t.cfg.hh=d.getHours();
            _t.cfg.ii=d.getMinutes();
            _t.cfg.ss=d.getSeconds();
        }

        _t.regDaySelect();
        _t.Pos = _t.getPos(obj);
        var objheight = obj.offsetHeight;
        _t.cfg.oiframe.style.left = _t.cfg.obody.style.left = _t.Pos.x+"px";
        _t.cfg.oiframe.style.top = _t.cfg.obody.style.top = _t.Pos.y+objheight+"px";
        if(_t.cfg.isShowTime==false){
            _t.$$("calender_hhiiss").style.display = "none";
        }else{
            _t.$$("calender_hhiiss").style.display = "block";
        }

        _t.cfg.obody.style.display = "block";
		if(_t.cfg.isIE){
			_t.cfg.oiframe.style.display = "block";
			_t.cfg.oiframe.style.width=_t.cfg.obody.offsetWidth+"px";
			_t.cfg.oiframe.style.height=_t.cfg.obody.offsetHeight+"px";
		}
     }
}
calender.prototype.regDaySelect = function(){
    var _t = this;
    var obj = _t.cfg.oT;
    _t.$$("calender_yy").value = _t.cfg.yy;
    _t.$$("calender_mm").value = _t.cfg.mm;
    _t.$$("calender_hh").value = _t.cfg.hh;
    _t.$$("calender_ii").value = _t.cfg.ii;
    _t.$$("calender_ss").value = _t.cfg.ss;
    var ddtds = _t.$$("calender_dd").getElementsByTagName("TD");
    var fromday = _t.getWeekDay();
    var totalDays = _t.getDaysInMonth();
    var d = new Date();
    for(var ddd=1;ddd<=totalDays;ddd++){
        var k = ddd+fromday-1;
        ddtds[k].innerHTML = ddd;
        ddtds[k].style.cursor = "pointer";
        if(ddd==_t.cfg.dd){
            ddtds[k].className="calender-select-td";
        }else{
            ddtds[k].className="";
        }
        if(d.getDate()==ddd && _t.cfg.yy==d.getFullYear() && _t.cfg.mm==(d.getMonth()+1)){
            ddtds[k].className = "calender-current-td";
        }
        ddtds[k].onclick =  function(){
            _t.cfg.dd = this.innerHTML;
            obj.value=_t.getSelect();
            _t.hide();
        };
    }
    var perMonthDays = _t.getDaysInPerMonth();
    for(var ddd=(fromday-1);ddd>=0;ddd--){
        ddtds[ddd].className="calender-select-disabled-td";
        ddtds[ddd].innerHTML = perMonthDays--;
        ddtds[ddd].onclick = null;
    }
    var auto=1;
    for(var ddd=(totalDays+fromday);ddd<(ddtds.length-2);ddd++){
        ddtds[ddd].className="calender-select-disabled-td";
        ddtds[ddd].innerHTML = auto++;
        ddtds[ddd].onclick = null;
    }
}
calender.prototype.hide = function(){
	var _t = this;
	
	if(typeof _t.cfg.oT!="object"){
		return;
	}
	_t.cfg.oT.value = _t.trim(_t.cfg.oT.value);
	if(_t.cfg.oT.value!=""  &&  _t.checkValid(_t.cfg.oT.value)==false){
		alert("时间格式不正确");
		_t.cfg.oT = null;
	}
	_t.cfg.obody.style.display = "none";
	_t.cfg.oiframe.style.display = "none";
}
calender.prototype.initTable = function(){
    var _t = this;
    var yy=_t.cfg.yy,mm=_t.cfg.mm,hh=_t.cfg.hh,ii=_t.cfg.ii,ss=_t.cfg.ss;
    var strYY = '<select id="calender_yy">';
    for(var i=yy-_t.cfg.Y_span;i<(yy+_t.cfg.Y_span);i++){
        strYY+='<option value="'+i+'"';
        if(i==yy){
            strYY+=' selected ';
        }
        strYY+='>'+i+'</option>';
    }
    strYY+='</select>';
    var strMM = '<select id="calender_mm">';
    for(var i=1;i<=12;i++){
        strMM+='<option value="'+i+'"';
        if(i==mm){
            strMM+=' selected ';
        }
        var i_display=i;
		if(i_display<10){
			i_display = "0"+i_display;
		}
        strMM+='>'+i_display+'</option>';
    }
    strMM+='</select>';
    var str = '<table class="calender_header" style="width:'+_t.cfg.tw+'">';
    str += '<tr><td align="left" id="calender_pre"></td><td colspan="3" align="center">'+strYY+'</td><td  align="center" colspan="2">'+strMM+'</td><td align="right" id="calender_next"></td></tr>';
    str += '</table><table style="width:'+_t.cfg.tw+'" id="calenderHeader">';
    str += '<tr title="按住鼠标移动窗口"><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>';
    str += '</table>';
    str += '<table style="width:'+_t.cfg.tw+'" id="calender_dd">';
    for(var r=0;r<5;r++){
        str += '<tr>';
        for(var i=0;i<7;i++){
            str += '<td></td>';
        }
        str += '</tr>';
    }
    str+='<tr><td></td><td></td><td colspan="5" align="center"><a href="javascript:void(0)" id="calender_clear" title="清空">清空</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" id="calender_current" title="当前时间">当前</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" id="calender_close" title="点击或者按ESC关闭">关闭</a></td></tr>';

	var strHH = '<select id="calender_hh">';
    for(var i=0;i<24;i++){
        strHH+='<option value="'+i+'"';
        if(i==hh){
            strHH+=' selected ';
        }
		var i_display=i;
		if(i_display<10){
			i_display = "0"+i_display;
		}
        strHH+='>'+i_display+'</option>';
    }
    strHH+='</select>';

	var strII = '<select id="calender_ii">';
    for(var i=0;i<60;i++){
        strII+='<option value="'+i+'"';
        if(i==ii){
            strII+=' selected ';
        }
        var i_display=i;
		if(i_display<10){
			i_display = "0"+i_display;
		}
        strII+='>'+i_display+'</option>';
    }
    strII+='</select>';
	var strSS = '<select id="calender_ss">';
    for(var i=0;i<60;i++){
        strSS+='<option value="'+i+'"';
        if(i==ss){
            strSS+=' selected ';
        }
       var i_display=i;
		if(i_display<10){
			i_display = "0"+i_display;
		}
        strSS+='>'+i_display+'</option>';
    }
    strSS+='</select>';
	str+='<tr><td colspan="7"><span id="calender_hhiiss" ';
    str+='>'+strHH+'时'+strII+'分'+strSS+'秒</span></td></tr>';
    str+='</table>';
    _t.cfg.obody.innerHTML = str;
    _t.$$("calender_pre").style.cursor = "pointer";
    _t.$$("calender_next").style.cursor = "pointer";
    _t.$$("calender_yy").onchange = function(){_t.cfg.yy = this.value;_t.onchange(1);}
    _t.$$("calender_mm").onchange = function(){_t.cfg.mm = this.value;_t.onchange(1);}
    _t.$$("calender_hh").onchange = function(){_t.cfg.hh = this.value;_t.onchange();}
    _t.$$("calender_ii").onchange = function(){_t.cfg.ii = this.value;_t.onchange();}
    _t.$$("calender_ss").onchange = function(){_t.cfg.ss = this.value;_t.onchange();}
}
calender.prototype.onchange = function(f){
    var _t = this;
    if(f==1){
        _t.regDaySelect();
    }
    if(_t.cfg.oT.value!=""){
        _t.cfg.oT.value=_t.getSelect();
    }
}
calender.prototype.getPos = function(d){
    var x=0,y=0;
    var el=d;
    while(el){
            x=x+el.offsetLeft;
            y=y+el.offsetTop;
            el=el.offsetParent;
            
    }
    return {"x":x,"y":y};
}
calender.prototype.getWeekDay = function(){//星期几
    var _t = this;
	var dt=new Date(_t.cfg.yy,_t.cfg.mm-1,1).getDay();return dt;
}
calender.prototype.getDaysInMonth = function(){//月份中总天数
    var _t = this;
    var mm = _t.cfg.mm-1;
    var mm_1 = _t.cfg.mm;
	var d1=new Date(_t.cfg.yy,mm,1);
    var d2=new Date(_t.cfg.yy,mm_1,1);
    return Math.ceil((d2-d1)/86400/1000);
}
calender.prototype.getDaysInPerMonth = function(){//上一月月份中总天数
    var _t = this;
    var yy = _t.cfg.yy;
    var mm = _t.cfg.mm-1;
    var d1=new Date(yy,mm,1);
    var mm_1 = mm-1;

    if(mm_1<0){
        mm_1 = 11;
        yy--;
    }
    var d2=new Date(yy,mm_1,1);
    return Math.ceil((d1-d2)/86400/1000);
}
calender.prototype.getSelect = function(){
    var _t = this;
	var mm=_t.cfg.mm>=10?_t.cfg.mm:("0"+parseInt(_t.cfg.mm));
	var dd=_t.cfg.dd>=10?_t.cfg.dd:("0"+parseInt(_t.cfg.dd));
        if(_t.cfg.oT.isShowType=='month'){
        return _t.cfg.yy+"-"+mm;
    }
    if(_t.cfg.isShowTime==false){
        return _t.cfg.yy+"-"+mm+"-"+dd;
    }
	var hh=_t.cfg.hh>=10?_t.cfg.hh:("0"+_t.cfg.hh);
	var ii=_t.cfg.ii>=10?_t.cfg.ii:("0"+_t.cfg.ii);
	var ss=_t.cfg.ss>=10?_t.cfg.ss:("0"+_t.cfg.ss);
    return _t.cfg.yy+"-"+mm+"-"+dd+" "+hh+":"+ii+":"+ss;
}
calender.prototype.checkValid = function(dt){
	var _t = this;
        if(_t.cfg.oT.isShowType=='month'){
		if(/^((\d{2}(([02468][048])|([13579][26]))[\-\/]?((((0?[13578])|(1[02])))|(((0?[469])|(11)))|(0?2)))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/]?((((0?[13578])|(1[02])))|(((0?[469])|(11)))|(0?2))))$/.test(dt)){
			return true;
		}
	}
	if(_t.cfg.isShowTime==true){
		if(/^((\d{2}(([02468][048])|([13579][26]))[\-\/]((((0?[13578])|(1[02]))[\-\/]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/]((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/]((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/]((((0?[13578])|(1[02]))[\-\/]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/]((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/]((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(?:0?\d|1\d|2[0-3])\:(?:0?\d|[1-5]\d)\:(?:0?\d|[1-5]\d))$/.test(dt)){
			return true;
		}
	}else{
		if(/^((\d{2}(([02468][048])|([13579][26]))[\-\/]?((((0?[13578])|(1[02]))[\-\/]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/]?((((0?[13578])|(1[02]))[\-\/]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/]?((0?[1-9])|(1[0-9])|(2[0-8]))))))$/.test(dt)){
			return true;
		}
	}
	return false;
}
calender.prototype.Drag = function(oHeader,event){
    var _t = this;
    var dragObj=_t.cfg.obody;
	var dragFrame=_t.cfg.oiframe;
    event = event ||window.event;
    var sx = event.x || event.clientX;
    var sy = event.y || event.clientY;
    var cx = parseInt(dragObj.offsetLeft);
    var cy = parseInt(dragObj.offsetTop);

    var scrollTop = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	var scrollLeft = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
	var scrollHeight = Math.max(document.body.clientHeight,document.documentElement.clientHeight)+scrollTop;	
	var scrollWidth = Math.max(document.body.clientWidth,document.documentElement.clientWidth)+scrollLeft;
	scrollWidth -= 5;
	scrollHeight -= 5;
	_t.addEvent(document,"mousemove",function(event){
        event = event ||window.event;
        if(_t.cfg.isDrag==false)return false;
        var iLeft = (cx + parseInt(event.x || event.clientX)- sx);
        if(iLeft<=scrollLeft)iLeft=scrollLeft;
        var iTop = (cy + parseInt(event.y || event.clientY)- sy);
        if(iTop<=scrollTop)iTop=scrollTop;

        if((iLeft+dragObj.offsetWidth)>=scrollWidth){
            iLeft = scrollWidth-dragObj.offsetWidth;
        }
        dragFrame.style.left = dragObj.style.left = iLeft+"px";
  		dragFrame.style.top  = dragObj.style.top  = iTop+"px";
    });
    _t.addEvent(document,"mouseup",function(event){_t.cfg.isDrag=false;this.onmouseup = null;this.onmouseover=null;});
}

calender.prototype.trim = function(s){
	if(typeof s==undefined || typeof s=="undefined"){
		return "";
	}
    return s.replace(/^\s+|\s+$/g,"");
}

calender.prototype.addEvent = function(el, evname, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} else {
		el["on" + evname] = func;
	}
};